/**
 * \file plane.hpp
 * 
 * \section COPYRIGHT
 *
 * Basis: A 3D Mathematics Library
 *
 * ---------------------------------------------------------------------
 *
 * Copyright (c) 2010, Don Olmstead
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *  3. Neither the name of organization nor the names of its contributors may be
 *     used to endorse or promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef BASIS_PLANE_HPP_INCLUDED
#define BASIS_PLANE_HPP_INCLUDED

#include <basis/vector3.hpp>

namespace basis
{
	/**
	 * Defines a plane.
	 *
	 * \tparam Real A real number type.
	 * \tparam InstructionSet The SIMD instruction set to use.
	 *
	 * \author Don Olmstead
	 * \version 0.1
	 */
	template <typename Real, typename InstructionSet = simd::none>
	class plane
	{

		//------------------------------------------------------------
		// Member types
		//------------------------------------------------------------

		public:

			/// Value of a real number
			typedef Real value_type;
			/// Reference to a real number
			typedef Real& reference;
			/// Value of a vector3
			typedef vector3<Real, InstructionSet> vector_type;
			/// Reference to a vector3
			typedef vector3<Real, InstructionSet>& vector_reference;
			/// Constant reference to a vector3
			typedef const vector3<Real, InstructionSet>& const_vector_reference;

		//------------------------------------------------------------
		// Construction/Destruction
		//------------------------------------------------------------

		public:

			/**
			 * Creates an instance of the plane class.
			 *
			 * Uninitialized to speed creation.
			 */
			plane()
			{ }

			/**
			 * Destroys an instance of the plane class.
			 */
			~plane()
			{ }

			/**
			 * Initializes an instance of the plane class.
			 *
			 * \param normal The normal vector to the plane.
			 * \param d The plane's distance along its normal from the origin.
			 */
			plane(const_vector_reference normal, value_type d)
				: _normal(normal)
				, _d(d)
			{ }

			/**
			 * Initializes an instance of the plane class.
			 *
			 * \param point0 A point of a triangle defining the plane.
			 * \param point1 A point of a triangle defining the plane.
			 * \param point2 A point of a triangle defining the plane.
			 */
			plane(const_vector_reference point0, const_vector_reference point1, const_vector_reference point2)
			{
				vector_type edge0 = point1 - point0;
				vector_type edge1 = point2 - point0;

				_normal = cross(edge0, edge1);
				_normal.normalize();

				_d = dot(_normal, point0);
			}

			/**
			 * Initializes an instance of the plane class.
			 *
			 * Creates a copy of the given instance.
			 *
			 * \param copy The plane to copy.
			 */
			plane(const plane& copy)
				: _vector(copy._vector)
				, _d(copy._d)
			{ }

			/**
			 * Assignment operator for the plane class.
			 *
			 * Copies the values from the inputed plane.
			 *
			 * \param copy The plane to copy.
			 * \returns A copy of the inputed plane.
			 */
			inline plane& operator= (const plane& copy)
			{
				_vector = copy._vector;
				_d      = copy._d;

				return *this;
			}

		//------------------------------------------------------------
		// Comparison operators
		//------------------------------------------------------------

		public:

			/**
			 * Determines whether two planes are equal.
			 *
			 * \param compare The plane to compare with.
			 * \returns \b true if the instances are equal; \b false otherwise.
			 */
			inline bool operator== (const plane& compare) const
			{
				return (_d == compare._d) && (_vector == compare._vector);
			}

			/**
			 * Determines whether two planes are not equal.
			 *
			 * \param compare The plane to compare with.
			 * \returns \b true if the instances are not equal; \b false otherwise.
			 */
			inline bool operator!= (const plane& compare) const
			{
				return (_d != compare._d) && (_vector != compare._vector);
			}

		//------------------------------------------------------------
		// Accessors
		//------------------------------------------------------------

		public:

			/**
			 * Retrieves the normal vector of the plane.
			 *
			 * \returns The normal vector of the plane.
			 */
			inline const_vector_reference normal() const
			{
				return _vector;
			}

			/**
			 * Retrieves the normal vector of the plane.
			 *
			 * \returns The normal vector of the plane.
			 */
			inline vector_reference normal()
			{
				return _vector;
			}

			/**
			 * Retrieves the distance of the plane along its normal from the origin.
			 *
			 * \returns The distance of the plane along its normal from the origin.
			 */
			inline value_type d() const
			{
				return _d;
			}

			/**
			 * Retrieves the distance of the plane along its normal from the origin.
			 *
			 * \returns The distance of the plane along its normal from the origin.
			 */
			inline reference d()
			{
				return _d;
			}

		//------------------------------------------------------------
		// Friend functions
		//------------------------------------------------------------

		public:

			/**
			 * Calculates the distance between a plane and a point.
			 *
			 * \tparam Real A real number type.
			 * \tparam InstructionSet The SIMD instruction set to use.
			 *
			 * \param plane_value The plane to use.
			 * \param point The point to use.
			 * \returns The distance from the point to the plane.
			 */
			template <typename Real, typename InstructionSet>
			friend inline Real distance(const plane<Real, InstructionSet>& plane_value, const vector3<Real, InstructionSet>& point)
			{
				return dot(plane_value._normal, point) - plane_value._d;
			}

		private:

			/// The normal vector of the plane.
			vector_type _normal;
			/// The distance of the plane along its normal from the origin.
			value_type _d;

	} ; // end class plane

} // end namespace basis

#endif // end BASIS_PLANE_HPP_INCLUDED
